/**
 * 
 */
package org.vsg.stock.basic.domain;

import java.util.Vector;

import org.vsg.common.biz.uid.IdGenerator;
import org.vsg.stock.basic.service.StockBusinessHelper;
import org.vsg.stock.core.domain.StockDailyKLine;

/**
 * @author Bill Vison
 *
 */
public class BalanceLineFactory {
	
	private static BalanceLineFactory factory = new BalanceLineFactory();
	
	
	private static IdGenerator idGen  = new IdGenerator();
	
	private BalanceLineFactory() {
		
	}
	
	
	public static BalanceLineFactory getInstance() {
		
		return factory;
	}
	
	
	public BalanceLineKeyPoint createPoint(double stanPrice , StockDailyKLine currrntPoint ) {
		
		// --- create key point ---
		
		BalanceLineKeyPoint blkPoint = new BalanceLineKeyPoint();

		
		String pointId = "KPO" +  idGen.generateId(13) ;
		blkPoint.setPointId( pointId );
		blkPoint.setStockCode( currrntPoint.getId().getStockCode() );
		blkPoint.setRefDate( currrntPoint.getDate() );
		
		
		double highDiff = Math.abs( stanPrice - currrntPoint.getHigh() );
		double openDiff = Math.abs( stanPrice - currrntPoint.getOpen() );
		double closeDiff = Math.abs( stanPrice - currrntPoint.getClose() );
		double lowDiff = Math.abs( stanPrice - currrntPoint.getLow());

		if (StockBusinessHelper.checkDiffScope(stanPrice, highDiff) ) {
			blkPoint.setPriceType( BalanceLineKeyPoint.HIGH );
			blkPoint.setPrice( currrntPoint.getHigh() );
			boolean substan = StockBusinessHelper.checkIsIntegerPrice( currrntPoint.getHigh() );
			blkPoint.setIntegerFlag( substan? (short)1 : (short)0 );
		}
		else if (StockBusinessHelper.checkDiffScope(stanPrice, lowDiff)) {
			blkPoint.setPriceType( BalanceLineKeyPoint.LOW );
			blkPoint.setPrice( currrntPoint.getLow() );
			boolean substan = StockBusinessHelper.checkIsIntegerPrice( currrntPoint.getLow() );
			blkPoint.setIntegerFlag( substan? (short)1 : (short)0 );
		}
		else if (StockBusinessHelper.checkDiffScope(stanPrice, openDiff)) {
			blkPoint.setPriceType( BalanceLineKeyPoint.OPEN );
			blkPoint.setPrice( currrntPoint.getOpen() );
			boolean substan = StockBusinessHelper.checkIsIntegerPrice( currrntPoint.getOpen() );
			blkPoint.setIntegerFlag( substan? (short)1 : (short)0 );
			blkPoint.setSubstanceFlag((short)1);
		}
		else if (StockBusinessHelper.checkDiffScope(stanPrice, closeDiff)) {
			blkPoint.setPriceType( BalanceLineKeyPoint.CLOSE );
			blkPoint.setPrice( currrntPoint.getClose() );
			boolean substan = StockBusinessHelper.checkIsIntegerPrice( currrntPoint.getClose() );
			blkPoint.setIntegerFlag( substan? (short)1 : (short)0 );
			blkPoint.setSubstanceFlag((short)1);
		}		
		
		return blkPoint;
	}

	
	public BalanceLine createLine(String stockCode , String form) {
		String id = "BAL" +idGen.generateId(10);
		
		BalanceLine bl = new BalanceLine();
		bl.setLineId(id);
		bl.setStockCode( stockCode );
		
		return bl;
	}
}
